﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.BIZ;
using TracNghiemTinHocDaiCuong.ThucThe;
using TracNghiemTinHocDaiCuong.Common;

namespace TracNghiemTinHocDaiCuong
{
    public partial class BaiHocOnLuyen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void tvBaiHoc_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                switch (e.Node.Depth)
                {
                    case 0:
                        XayDungCay(e.Node, null);
                        break;
                    case 3:
                        //NutLa(e.Node);
                        break;
                }
            }
        }

        bool KiemTraNutLa(BaiHoc baiHoc)
        {
            string query = string.Format("select MaBH " +
                            "FROM BaiHoc " +
                            "WHERE MaBH NOT IN " +
                            "( " +
                            "	SELECT MaBaiTiep " +
                            "	FROM BaiHoc " +
                            "	WHERE MaBaiTiep IS NOT NULL " +
                            ")");
            DataTable dt = SqlHelper.ExecuteQuery(query);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (baiHoc.MaBH == Methods.ConvertNumber(row["MaBH"].ToString()))
                        return true;
                }
            }
            return false;
        }

        void XayDungCay(TreeNode node, BaiHoc baiHocCha)
        {
            if (baiHocCha == null)
            {
                string query = string.Format("SELECT TenBH, MaBH, MaBaiTiep  FROM BaiHoc WHERE MaBaiTiep IS NULL");
                DataTable dt = SqlHelper.ExecuteQuery(query);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        TreeNode NewNode = new TreeNode(row["TenBH"].ToString(), row["MaBH"].ToString());
                        //NewNode.PopulateOnDemand = true;
                        NewNode.SelectAction = TreeNodeSelectAction.Expand;
                        node.ChildNodes.Add(NewNode);
                        BaiHoc baiHocMoi = new BaiHoc();
                        baiHocMoi.MaBH = Methods.ConvertNumber(row["MaBH"].ToString());
                        baiHocMoi.TenBH = row["TenBH"].ToString();
                        baiHocMoi.MaBaiTiep = Methods.ConvertNumber(row["MaBaiTiep"].ToString());
                        XayDungCay(NewNode, baiHocMoi);
                    }
                }
            }
            else
            {
                string query = string.Format("SELECT TenBH, MaBH, MaBaiTiep  FROM BaiHoc WHERE MaBaiTiep=@MaBaiTiep ");
                var parameter = new Collection<SqlParameter>
                {
                    new SqlParameter("@MaBaiTiep", baiHocCha.MaBH)
                };
                DataTable dt = SqlHelper.ExecuteQuery(query, parameter);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        TreeNode NewNode = new TreeNode(row["TenBH"].ToString(), row["MaBH"].ToString());
                        //NewNode.PopulateOnDemand = true;
                        NewNode.SelectAction = TreeNodeSelectAction.Expand;
                        node.ChildNodes.Add(NewNode);
                        BaiHoc baiHocMoi = new BaiHoc();
                        baiHocMoi.MaBH = Methods.ConvertNumber(row["MaBH"].ToString());
                        baiHocMoi.TenBH = row["TenBH"].ToString();
                        baiHocMoi.MaBaiTiep = Methods.ConvertNumber(row["MaBaiTiep"].ToString());
                        if (KiemTraNutLa(baiHocMoi))
                            NewNode.NavigateUrl = string.Format("NoiDungBaiHoc.aspx?ID={0}", baiHocMoi.MaBH);
                        XayDungCay(NewNode, baiHocMoi);
                    }
                }
            }
        }
    }
}